package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.Usertree;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.system.mapper.UsertreeMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.UserTreeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserTreeServiceImpl extends BaseServiceImpl<UsertreeMapper, Usertree> implements UserTreeService {


    @Transactional
    public void delUserTree(Long userId) {
        Usertree userTree = new Usertree();
        userTree.setDist(2);
        userTree.setParentId(userId);
        //查询是否有下级  如果有下级 不允许修改邀请人
        Usertree ut = this.getDomain(userTree);
        if (ut == null) {
            throw new BusiException("邀请人已存在");
        }


        QueryWrapper<Usertree> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", userId);
        this.baseMapper.delete(queryWrapper);

        queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        this.baseMapper.delete(queryWrapper);
    }


}
